<template>
    <div id="subLine-controller">
        <div class="item-wrapper">
            <div
                class="item"
                v-for="item in subLineData"
                @click="handleItemClick(item)"
            >
                <div
                    class="box"
                    :style="{
                        borderColor: item.color,
                        backgroundColor: item.color,
                    }"
                ></div>
                <span :style="{ color: '#34c5cf' }">{{
                    item.name.slice(-3)
                }}</span>
            </div>
        </div>
    </div>

    <div class="active-panel">
        <div class="header">
            <strong>重点活动</strong>
            <span>{{ date }}</span>
        </div>
        <div class="content">
            <div class="active-wrapper" v-if="activityData.length">
                <div
                    v-for="item in activityData"
                    :class="['acitve-item', item.isActive ? 'item-active' : '']"
                    @click="clickActiveItem(item)"
                >
                    <div>{{ item.activedTime }}</div>
                    <div>{{ item.activedPlace }}</div>
                    <div>{{ item.activity }}</div>
                </div>
            </div>
            <a-empty v-else :image="simpleImage" description="无数据" />
        </div>
    </div>
</template>

<script setup>
import { useLineData,watchLineData } from '@/store'
import { getLine } from '@/api/line.js'
import { onBeforeMount, onBeforeUnmount, onMounted, ref } from 'vue'
import { activity } from '@/store/staticData.js'
import {
  flyToLine,
  addGradientCone,
  removeAllCones,
  binkLineByName,
  renderLines,
} from '../../cesiumTools/LineMaterial/effectController'
import * as Cesium from 'cesium'
const activityData = ref([])
let subLineData = ref([])

// 从pinia中拿到viewer
const $store = useLineData();
const viewer = $store.viewer;

const currentStations = ref([])
onMounted(async() => {
  await watchLineData().then((res) => {
    subLineData.value = res
  })
})
const handleItemClick = (item) => {
  flyToLine(viewer, item.name)
  binkLineByName(item.name)
  currentStations.value = item.stationsList
  // 拿到四个重保的数据
  activityData.value = activity.filter(
    (e) => e.activedLine == item.name.slice(-3)
  )
  showActiveArea(item.color)
}

const clickActiveItem = (item) => {
  const { activedLine, id } = item
  console.log(activedLine, id)
  // 拿到当前选中的线路的站点坐标
  const condition = subLineData.value.filter(
    (item) => item.name.slice(-3) == activedLine
  )[0].stationsList[id].position
  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(
      condition.lng-0.025,
      condition.lat-0.04,
      6000
    ),
    orientation: {
      pitch: Cesium.Math.toRadians(-45.0),
      heading: Cesium.Math.toRadians(30.0),
    },
  })
}

const showActiveArea = (color) => {
    // 先清除之前的圆柱
    removeAllCones(viewer);
    const ids = activityData.value.map((item) => item.id);
    // 这里坐标直接取index和id对应的站点坐标
    const activedStations = currentStations.value
        .map((item, index) => {
            item.id = index;
            return item;
        })
        .filter((item) => ids.includes(item.id));
    activedStations.forEach((item) => {
        const { position, id } = item;
        // 添加渐变圆柱
        addGradientCone(viewer, {
            position,
            color,
            name: id,
        });
    });
};
onBeforeUnmount(() => {
  removeAllCones(viewer)
})
</script>

<style scoped>
#subLine-controller {
    position: absolute;
    bottom: 10%;
    left: 34%;
    transform: translateX(-50%);
    width: 146px;
    height: 140px;
    background-color: rgba(0, 0, 0, 0.3);
    border: 1px solid #664a16;
    padding: 4px;
    display: flex;
    flex-direction: column;
}

.box {
    width: 10px;
    height: 10px;
    border-width: 1px;
    border-style: solid;
    background: transparent;
    user-select: all;
    cursor: pointer;
}

.item {
    width: 65px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 3px;
    background-color: rgba(255, 255, 255, 0.2);
    border: 1px solid #885f12;
    color: #fff;
    font-size: 10px;
    pointer-events: all;
    cursor: pointer;
}

.item:hover {
    border: 1px solid #d8961a;
}

.item > span {
    line-height: 9.005px;
}

.item > input {
    outline: none;
    border: none;
    transition: all 0.3s ease;
}

.item-wrapper {
    display: flex;
    justify-content: space-between;
    align-content: space-around;
    flex-wrap: wrap;
    flex: 1;
}

.active-panel {
    width: 500px;
    height: 140.006px;
    border: 1px solid #ab7818;
    background-color: rgba(0, 0, 0, 0.3);
    position: absolute;
    left: 39%;
    bottom: 10%;
    color: #fff;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.active-panel > .header {
    height: 30px;
    padding: 4px;
    background: rgb(255, 255, 255);
    background-image: linear-gradient(
        180deg,
        rgba(255, 255, 255, 1) 9%,
        rgba(211, 156, 50, 1) 57%
    );
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: -0.521vw;
    font-family: "等线Bold";
}

.active-panel > .header > strong {
    font-weight: normal;
    margin-right: 10px;
}

.active-panel > .header > span {
    font-size: 12px;
}

.content {
    flex: 1;
}

.active-wrapper {
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 100%;
}

.acitve-item {
    width: 150px;
    height: 70px;
    border: 1px solid #ab7818;
    background-color: rgba(214, 174, 41, 0.1);
    margin: 4px;
    text-align: center;
    pointer-events: all;
    cursor: pointer;
    transition: all 0.3s linear;
    color: #37b3bb;
    padding-top: 12px;
    font-size: 8px;
}

.acitve-item:hover {
    background-color: rgba(214, 174, 41, 0.3);
    color: #fff;
}

.item-active {
    background-color: rgba(214, 174, 41, 0.3);
    color: #fff;
}
</style>
